/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.psc.controllers;

import com.mycompany.psc.ejb.ReportableItemsManager;
import com.mycompany.psc.entities.Carpenter;
import com.mycompany.psc.entities.CarpenterOffers;
import com.mycompany.psc.entities.MetalDoors;
import com.mycompany.psc.entities.MetalDoorsOffers;
import com.mycompany.psc.entities.Users;
import com.mycompany.psc.producers.LoggedIn;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author A7med
 */
@Named
@SessionScoped
public class CarpenterController implements Serializable {

    @EJB
    ReportableItemsManager rm;
    @Inject
    ProjectsBean p;
    private String title;
    private String code;
    @Inject
    @LoggedIn
    Users current;
    private Carpenter selectedResource;
    private CarpenterOffers editableItem;
    private CarpenterOffers newItem = new CarpenterOffers();
    private float rate;
    private int amount;
    private String dimension;

    public CarpenterOffers getEditableItem() {
        return editableItem;
    }

    public void setEditableItem(CarpenterOffers editableItem) {
        this.editableItem = editableItem;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Carpenter getSelectedResource() {
        return selectedResource;
    }

    public void setSelectedResource(Carpenter selectedResource) {
        this.selectedResource = selectedResource;
    }

    public CarpenterOffers getNewItem() {
        return newItem;
    }

    public void setNewItem(CarpenterOffers newItem) {
        this.newItem = newItem;
    }

    public float getRate() {
        return rate;
    }

    public void setRate(float rate) {
        this.rate = rate;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

   

    public String getDimension() {
        return dimension;
    }

    public void setDimension(String dimension) {
        this.dimension = dimension;
    }
    public void updateItem(){
        rm.update(selectedResource);
    }
    public void updateChildItem(){
        rm.update(editableItem);
    }
    public void save() {
        Carpenter t = new Carpenter();
        t.setAuthor(current.getName());
        t.setProject(p.getSelectedReportProject());
        t.setSubmitDate(new Date());
        t.setDimension(dimension);
        t.setQuantity(amount);
        t.setRate(rate);
        t.setCode(code);

        t.setTitle(title);
        rm.persistObject(t);
    }

    public void saveData() {
        newItem.setParent(selectedResource);
        selectedResource.getCarpenterOffersList().add(newItem);
        rm.update(getSelectedResource());
    }

    public List<Carpenter> getCarResources() {
        return rm.getCarpenterResources(p.getSelectedReportProject());
    }
     public List<CarpenterOffers> getChildResources() {
        List<Carpenter> list = rm.getCarpenterResources(p.getSelectedReportProject());
        List<CarpenterOffers> out=new ArrayList<>();
        for(Carpenter ti:list){
            out.addAll(ti.getCarpenterOffersList());
        }
        return out;
    }
}
